package yanglib.application.metadata.message;

import yanglib.message.Message;
import yanglib.session.Request;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 9/6/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class GetMessage extends Request {
    private byte[] key;
    public GetMessage(ByteBuffer buffer) {
        super(buffer);
    }

    public GetMessage(int sender, byte[] key) {
        super(MetadataMessageDeserializer.GET_MESSAGE, sender);
        this.key = key;
    }

    public byte[] getKey(){
        return this.key;
    }

    @Override
    public void serialize(ByteBuffer buffer){

        super.serialize(buffer);
        buffer.putInt(key.length);
        buffer.put(key);
    }

    @Override
    public void deserialize(ByteBuffer buffer) throws IOException {
        super.deserialize(buffer);
        key = new byte[buffer.getInt()];
        buffer.get(key);
    }

    @Override
    public String toString(){
        return "Get " +getRequestID() + " key=" + Arrays.toString(key);
    }
}
